jQuery(document).ready(function() {
    jQuery("#message-form").validate({
        rules: {
            content: "required"
        },
        messages: {
            content: "Thông tin cần nhập!"
        }
    });
    
    // Datetime picker
    jQuery('#post-time').datetimepicker({
        dateFormat: 'dd/mm/yy',
        minDate: new Date()
    });

    jQuery('#id_user').keypress(function(evt) {
        evt = (evt) ? evt : window.event;
        var charCode = (evt.which) ? evt.which : evt.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            return false;
        }
        return true;
    });

    jQuery('#cb-user').change(function() {
        if (jQuery(this).is(":checked")) {
            jQuery('#send-to-user').fadeIn();
        }
        else {
            jQuery('#send-to-user').fadeOut();
            jQuery('#id_user').val('');
        }
    });
    
    jQuery('#cb-post-later').change(function() {
        if (jQuery(this).is(":checked")) {
            jQuery('#post-later').fadeIn();
        }
        else {
            jQuery('#post-later').fadeOut();
            jQuery('#post-time').val('');
        }
    });

    jQuery('#fileupload').fileupload({
        dataType: 'json',
        progressall: function(e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            jQuery('#progress-upload').fadeIn();
            jQuery('#progress-upload .bar .bluebar').css(
                    'width',
                    progress + '%'
                    );
            
        },
        done: function(e, data) {            
            jQuery.each(data.result.files, function(index, file) {
                jQuery('#file_url').val('');
                if (file.url) {
                    jQuery('#file_wrapper').html('');
                    jQuery('#img-avatar').attr('src', '');
                    jQuery('#file_url').val(file.url);
                    jQuery('#file_type').val('');

                    var audio_arr = ["audio/basic", "audio/L24", "audio/mp4", "audio/mpeg", "audio/mp3", "audio/ogg", "audio/vnd.wave"];
                    var image_arr = ["image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/svg+xml", "image/tiff"];

                    if (jQuery.inArray(file.type, image_arr) > 0) {
                        jQuery('#img-avatar').attr('src', file.url);
                        jQuery('#file_type').val('image');
                    }
                    if (jQuery.inArray(file.type, audio_arr) > 0) {
                        var content = '<audio controls="controls" id="audio">';
                        content += '<source src="' + file.url + '" type="audio/mpeg">';
                        content += '<source src="' + file.url + '" type="audio/ogg">';
                        content += '<source src="' + file.url + '" type="audio/mp3">';
                        content += 'Your browser does not support this audio format.';
                        content += '</audio>';
                        jQuery('#file_wrapper').html(content);
                        jQuery('#file_type').val('audio');
                    }
                }
            });
            jQuery('#progress-upload').fadeOut();
        }
    });
});